import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/changePass',
    component: () => import('@/views/changePass/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {title: '首页', icon: 'dashboard'}
    }]
  }, {
    path: '/info',
    component: Layout,
    children: [{
      path: '/info',
      name: 'info',
      component: () => import('@/views/myInfo/index'),
      meta: {title: '我的信息', icon: 'el-icon-user-solid'}
    }]
  },
  {
    path: '/workOrderSys',
    component: Layout,
    redirect: '/workOrderSys/todoOrder',
    name: 'WorkOrderSys',
    meta: {title: '工单系统', icon: 'el-icon-s-help'},
    children: [
      {
        path: 'chooseTemplate',
        name: 'chooseTemplate',
        component: () => import('@/views/creatOrder/chooseTemplate'),
        meta: {title: '创建工单', icon: 'el-icon-document-add', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'creatOrder',
        hidden: true,
        name: 'CreatOrder',
        component: () => import('@/views/creatOrder/index'),
        meta: {title: '创建工单', icon: 'el-icon-document-add', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'creatMultipleOrder',
        name: 'creatMultipleOrder',
        component: () => import('@/views/creatMultipleOrder/index'),
        meta: {
          title: '批量创建工单',
          icon: 'el-icon-document-add',
          role: ['generalInspector', 'zInspector', 'qaInspector']
        }
      },
      {
        path: 'myDraft',
        name: 'MyDraft',
        component: () => import('@/views/myDraft/index'),
        meta: {title: '我的草稿', icon: 'el-icon-edit-outline', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'editDraft',
        name: 'editDraft',
        hidden: true,
        component: () => import('@/views/myDraft/editDraft'),
        meta: {title: '编辑草稿', icon: 'el-icon-document', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'myCreation',
        name: 'MyCreation',
        component: () => import('@/views/myCreation/index'),
        meta: {title: '我创建的', icon: 'el-icon-document', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'orderDetail',
        hidden: true,
        name: 'orderDetail',
        component: () => import('@/views/orderDetail/index'),
        meta: {title: '工单详情', icon: 'el-icon-document-add', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'todoOrder',
        name: 'TodoOrder',
        component: () => import('@/views/todoOrder/index'),
        meta: {
          title: '待办工单',
          icon: 'el-icon-document-checked',
          role: ['generalInspector', 'zInspector', 'qaInspector']
        }
      },
      {
        path: 'myRelated',
        name: 'MyRelated',
        component: () => import('@/views/myRelated/index'),
        meta: {
          title: '我相关的',
          icon: 'el-icon-document-copy',
          role: ['generalInspector', 'zInspector', 'qaInspector']
        }
      },
      {
        path: 'archiveOrder',
        name: 'archiveOrder',
        component: () => import('@/views/archiveOrders/index'),
        meta: {title: '归档工单', icon: 'el-icon-s-check', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'getOrder',
        name: 'getOrder',
        component: () => import('@/views/getOrder/index'),
        meta: {title: '认领工单', icon: 'el-icon-question', role: ['generalInspector', 'zInspector', 'qaInspector']}
      },
      {
        path: 'problemOrder',
        name: 'problemOrder',
        component: () => import('@/views/problemOrder/index'),
        meta: {
          title: '问题工单',
          icon: 'el-icon-warning-outline',
          role: ['generalInspector', 'zInspector', 'qaInspector']
        }
      },
      {
        path: 'childOrderView/:ftid',
        name: 'childOrderView',
        hidden: true,
        component: () => import('@/views/childOrderView/index'),
        meta: {title: '子工单集合', icon: 'el-icon-finished'}
      }
    ]
  },
  {
    path: '/sapSys',
    component: Layout,
    redirect: '/sapSys/sapManage',
    name: 'SapSys',
    meta: {title: 'SAP管理', icon: 'el-icon-s-operation', role: 1},
    children: [
      {
        path: 'sapManage',
        name: 'sapManage',
        component: () => import('@/views/sapManage/index'),
        meta: {title: 'Sap订单管理', icon: 'el-icon-edit', role: 1}
      },
      {
        path: 'sapOrder',
        name: 'sapOrder',
        component: () => import('@/views/sapOrder/index'),
        meta: {title: '订单详情', icon: 'el-icon-edit', role: 1}
      }
    ]
  },
  {
    path: '/staffingSys',
    component: Layout,
    redirect: '/staffingSys/userManage',
    name: 'StaffingSys',
    meta: {title: '系统管理', icon: 'el-icon-s-operation', role: 1},
    children: [
      {
        path: 'lineManage',
        name: 'lineManage',
        component: () => import('@/views/lineManage/index'),
        meta: {title: '产线管理', icon: 'el-icon-s-operation', role: 1}
      },
      {
        path: 'userManage',
        name: 'UserManage',
        component: () => import('@/views/userManage/index'),
        meta: {title: '用户管理', icon: 'users', role: 1}
      },
      {
        path: 'userDetail',
        hidden: true,
        name: 'UserDetail',
        component: () => import('@/views/userDetail/index'),
        meta: {title: '用户详情', icon: 'users', role: 1}
      },
      // {
      //   path: 'departmentManage',
      //   name: 'DepartmentManage',
      //   component: () => import('@/views/departmentManage/index'),
      //   meta: { title: '部门管理', icon: 'el-icon-s-grid', role: 1 }
      // },
      {
        path: 'orderManage',
        name: 'OrderManage',
        component: () => import('@/views/orderManage/index'),
        meta: {title: '工单管理', icon: 'ticketsmanage', role: 1}
      },
      {
        path: 'orderTemplate',
        name: 'OrderTemplate',
        component: () => import('@/views/orderTemplate/index'),
        meta: {title: '模板管理', icon: 'templatesmanage', role: 1}
      },
      {
        path: 'editTemplate',
        hidden: true,
        name: 'EditTemplate',
        component: () => import('@/views/editTemplate/index'),
        meta: {title: '编辑模板', icon: 'el-icon-circle-plus-outline', role: 1}
      },
      {
        path: 'problemManage',
        name: 'ProblemManage',
        component: () => import('@/views/problemManage/index'),
        meta: {title: '问题管理', icon: 'el-icon-document-delete', role: 1}
      },
      {
        path: 'orderCount',
        name: 'orderCount',
        component: () => import('@/views/orderCount/index'),
        meta: {title: '工单统计', icon: 'el-icon-finished', role: 1}
      }
    ]
  },
  {
    path: '/editForm',
    component: Layout,
    redirect: '/staffingSys/formManage',
    name: 'StaffingSys',
    meta: {title: '系统管理', icon: 'el-icon-s-operation', role: 1},
    children: [
      {
        path: 'formManage',
        name: 'editForm',
        component: () => import('@/views/editForm/index'),
        meta: {title: '工单自定义', icon: 'el-icon-edit', role: 1}
      }
    ]
  },
  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

/* roles: ['admin','generalInspector', 'zInspector', 'qaInspector'] */

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
